package com.mvc4g.example.client.main;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.mvc4g.client.Controller;
import com.mvc4g.client.Event;
import com.mvc4g.client.ViewInterface;

public class MainView extends Composite implements ViewInterface {

	private DecoratorPanel body = new DecoratorPanel();
	private Controller controller = null;
	private Label message = new Label();

	public MainView() {
		VerticalPanel mainPanel = new VerticalPanel();

		mainPanel.add(initMenu());
		message.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		message.setStyleName("message");
		mainPanel.add(message);

		body.setPixelSize(300, 100);
		mainPanel.add(body);

		RootPanel.get().add(mainPanel);
		

	}

	public void setController(Controller controller) {
		this.controller = controller;
	}

	public void changePage(Widget page) {
		body.clear();
		page.setVisible(true);
		body.add(page);
		hideMessage();
	}

	public void showMessage(String message) {
		this.message.setText(message);
		this.message.setVisible(true);
	}

	public void hideMessage() {
		message.setVisible(false);
	}

	private TabBar initMenu() {
		TabBar menu = new TabBar();
		menu.addTab("Company");
		menu.addTab("Customer");
		menu.addTab("Products");
		
		menu.selectTab(0);

		menu.addSelectionHandler(new SelectionHandler<Integer>() {

			private String[] actions = { "homeCompany", "homeCustomer",
					"homeProduct" };

			@Override
			public void onSelection(SelectionEvent<Integer> event) {
				controller.handleEvent(new Event(actions[event
						.getSelectedItem()], null));
			}

		});

		return menu;
	}

}
